package capitulo2;

/****************************************************
 * Casteo de variables de referencia (Objetivo 5.2)
 ****************************************************/
class Animal{
	void makeNoise(){ System.out.println("generic Noise......"); }
}


class Dog extends Animal{
	void makeNoise() {System.out.println("bark"); }
    void playDead() { System.out.println(" roll over"); }
}


public class Tema12_Cast01 {

	public static void main(String[] args) {

		Animal[] a = {new Animal(), new Dog(), new Animal()};
		
		for (Animal animal : a) {
			animal.makeNoise();
			if(animal instanceof Dog){
//				 animal.playDead(); //Intenta hacer un comportamiento de Dog
				Dog d = (Dog)animal; // Casteo de la varible de referencia - downcasting
				d.playDead();
			}
		}
		
		System.out.println("*************************************\n");
		Dog d = new Dog();
        Animal a1 = d; // upcast no explicitito
        Animal a2 = (Animal) d; // upcast explicito
		System.out.println(a1 + " - " + a2);
		
		System.out.println("*************************************\n");
		Animal a3 = new Dog();
		Dog dog = (Dog) a3; // Es correcto pero no tecnicamente
		a3.makeNoise();
		
		// Este si es "sauber"  - ((Dog)a3).makeNoise();
		
		System.out.println("**************************************");
		((Dog)a3).makeNoise();
		((Dog)a3).playDead();
	}
	
}
